package com.dandelion.business.controller;


import cn.hutool.json.JSONUtil;
import com.dandelion.business.entity.SysUser;
import com.dandelion.business.entity.vo.user.CurrentUser;
import com.dandelion.business.entity.vo.user.req.BindRoleReqVO;
import com.dandelion.business.entity.vo.user.req.QueryUserListReqVO;
import com.dandelion.business.entity.vo.user.req.UserReqVO;
import com.dandelion.business.entity.vo.validationgroup.BaseInsert;
import com.dandelion.business.entity.vo.validationgroup.BaseUpdate;
import com.dandelion.business.service.ISysUserRoleService;
import com.dandelion.business.service.ISysUserService;
import com.dandelion.framework.annotations.CUAnnotation;
import com.dandelion.framework.common.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;


@Api(tags = "用户模块")
@Slf4j
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysUserRoleService userRoleService;


    @ApiOperation("新增用户")
    @PostMapping("/add")
    //@PreAuthorize("hasAuthority('homepage')") //需要主页的权限
    public R<Boolean> addUser(@ApiIgnore @CUAnnotation CurrentUser currentUser,
                              @RequestBody @Validated(value = {BaseInsert.class}) UserReqVO req
    ) {
        log.info("新增用户,用户id:{},参数:{}", currentUser.getUserId(), JSONUtil.toJsonStr(req));
        return userService.add(currentUser, req);
    }

    @ApiOperation("获取用户")
    @GetMapping("/query")
    //@PreAuthorize("hasAuthority('homepage')") //需要主页的权限
    public R<SysUser> queryUser(Long id) {
        log.info("获取用户,用户id:{}", id);
        return userService.queryUser(id);
    }


    @ApiOperation("修改用户")
    @PostMapping("/edit")
    //@PreAuthorize("hasAuthority('homepage')") //需要主页的权限
    public R<Boolean> editUser(@ApiIgnore @CUAnnotation CurrentUser currentUser,
                               @RequestBody @Validated(value = {BaseUpdate.class}) UserReqVO req
    ) {
        log.info("修改用户,用户id:{},参数:{}", currentUser.getUserId(), JSONUtil.toJsonStr(req));
        return userService.editUser(currentUser, req);
    }


    @ApiOperation("绑定角色")
    @PostMapping("/bindRole")
    //@PreAuthorize("hasAuthority('homepage')") //需要主页的权限
    public R<Boolean> bindRole(@RequestBody BindRoleReqVO req) {
        log.info("bindRole,参数:{}", JSONUtil.toJsonStr(req));
        return userRoleService.bindRole(req);
    }


    @ApiOperation("用户集合")
    @PostMapping("/userList")
    //@PreAuthorize("hasAuthority('homepage')") //需要主页的权限
    public R userList(@RequestBody QueryUserListReqVO req) {
        log.info("用户集合,参数:{}", JSONUtil.toJsonStr(req));
        return userService.userList(req);
    }


}
